from django.http import HttpResponse
from django.shortcuts import render
from blog.models import User
import hashlib


# Create your views here.
def login_page(request):
    return render(request, 'login.html')


def reg_view(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    elif request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        md = hashlib.md5()
        md.update(password.encode())
        password = md.hexdigest()
        # print(username, password)
        old_users = User.objects.filter(username=username)
        if old_users:
            return HttpResponse('用户名己注册')
        User.objects.create(username=username, password=password)
        user_obj=User.objects.get(username=username)
        request.session['username']=username
        request.session['uid']=user_obj.id
        return HttpResponse('注册成功')
